﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Data;
using ElingOA6_Admin_DSJCJ.Common;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{

    /// <summary>
    /// 友情连接管理
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PeerReviewQueryController : Controller
    {
        //
        // GET: /Admin/PeerReviewQuery/

        public ActionResult Index()
        {
            BLL.PeerReview PeerReviewBLL = new BLL.PeerReview();
            List<Model.PeerReview> PeerReviewModel = PeerReviewBLL.GetList();
            if (PeerReviewModel.Count > 0)
            {
                ViewData["title"] = PeerReviewModel[0].FTitle;
                if (PeerReviewModel[0].FIsLookIPControl == "N")
                {
                    ViewData["table"] = GetTable();
                }
                else 
                {
                    BLL.PeerReviewIP PeerReviewIPBLL = new BLL.PeerReviewIP();
                    List<Model.PeerReviewIP> PeerReviewIPList = PeerReviewIPBLL.GetListPeerReviewIP(PeerReviewModel[0].FPeerReviewID);

                    List<Model.PeerReviewIP> selectPeerReviewIPList = (from nm in PeerReviewIPList where nm.FProp == "2" select nm).ToList();
                    string ipList = "";
                    foreach (Model.PeerReviewIP ipModel in selectPeerReviewIPList)
                    {
                        if (ipList != "")
                        {
                            ipList += "," + ipModel.FIPGroupID.ToString();
                        }
                        else
                        {
                            ipList += ipModel.FIPGroupID.ToString();
                        }
                    }

                    if (Fetch.IpCheck("1", ipList))
                    {
                        ViewData["table"] = GetTable();
                    }
                    else
                    {
                        ViewData["table"] = "";
                        ViewData["jsAlert"] = "IP受限制不允许访问";
                    }
                }
            }
            else
            {
                ViewData["table"] = "";
                ViewData["jsAlert"] = "评议细则未定义";
            }
            return View();
        }

        public List<Models.PeerReviewQuery> PeerReviewQueryGetList(DataTable dt)
        {
            List<Models.PeerReviewQuery> PeerReviewQueryList = new List<Models.PeerReviewQuery>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Models.PeerReviewQuery model = new Models.PeerReviewQuery();
                if (dt.Rows[i]["FBeenVoterEmplNo"] != null)
                {
                    model.FBeenVoterEmplNo = dt.Rows[i]["FBeenVoterEmplNo"].ToString();
                }
                if (dt.Rows[i]["FPeerReviewRuleCd"] != null)
                {
                    model.FPeerReviewRuleCd = dt.Rows[i]["FPeerReviewRuleCd"].ToString();
                }
                if (dt.Rows[i]["FPeerReviewRuleScoreCd"] != null)
                {
                    model.FPeerReviewRuleScoreCd = dt.Rows[i]["FPeerReviewRuleScoreCd"].ToString();
                }
                if (dt.Rows[i]["FScore"] != null && dt.Rows[i]["FScore"].ToString()!="")
                {
                    model.FScore = decimal.Parse(dt.Rows[i]["FScore"].ToString());
                }
                if (dt.Rows[i]["sumcount"] != null && dt.Rows[i]["sumcount"].ToString() != "")
                {
                    model.sumcount = decimal.Parse(dt.Rows[i]["sumcount"].ToString());
                }
                if (dt.Rows[i]["sumfen"] != null && dt.Rows[i]["sumfen"].ToString() != "")
                {
                    model.sumfen = decimal.Parse(dt.Rows[i]["sumfen"].ToString());
                }
                PeerReviewQueryList.Add(model);
            }
            return PeerReviewQueryList;
        }

        public System.Web.Mvc.FileResult SaveExcel()
        {
            BLL.PeerReview PeerReviewBLL = new BLL.PeerReview();
            List<Model.PeerReview> PeerReviewModel = PeerReviewBLL.GetList();

            if (PeerReviewModel.Count > 0)
            {
                return File(CreateXLS(), "application/vnd.ms-excel", PeerReviewModel[0].FTitle);
            }
            else
            {
                return null;
            }
        }

        private Byte[] CreateXLS()
        {
            Byte[] arr = new Byte[1];

             BLL.PeerReview PeerReviewBLL = new BLL.PeerReview();
            StringBuilder str = new StringBuilder();
            List<Model.PeerReview> PeerReviewModel = PeerReviewBLL.GetList();

            if (PeerReviewModel.Count > 0)
            {
                BLL.PeerReviewRule PeerReviewRuleBLL = new BLL.PeerReviewRule();
                List<Model.PeerReviewRule> PeerReviewRuleList = PeerReviewRuleBLL.GetModelList(" FPeerReviewID = '" + PeerReviewModel[0].FPeerReviewID + "'");

                BLL.PeerReviewRuleOption PeerReviewRuleOptionBLL = new BLL.PeerReviewRuleOption();
                List<Model.PeerReviewRuleOption> PeerReviewRuleOptionList = PeerReviewRuleOptionBLL.GetModelList("");

                BLL.PeerReviewBeenVoterCls PeerReviewBeenVoterClsBLL = new BLL.PeerReviewBeenVoterCls();
                List<Model.PeerReviewBeenVoterCls> PeerReviewBeenVoterClsList = PeerReviewBeenVoterClsBLL.GetAll();

                BLL.PeerReviewBeenVoter PeerReviewBeenVoterBLL = new BLL.PeerReviewBeenVoter();
                List<Model.PeerReviewBeenVoter> PeerReviewBeenVoterList = PeerReviewBeenVoterBLL.GetAllList("");

                BLL.PeerReviewResult PeerReviewResultBLL = new BLL.PeerReviewResult();
                DataSet dt = PeerReviewResultBLL.GetQueryList("");

                List<Models.PeerReviewQuery> peerReviewResultList = PeerReviewQueryGetList(dt.Tables[0]);

                BLL.PeerReviewVoter PeerReviewVoterBLL = new BLL.PeerReviewVoter();
                List<Model.PeerReviewVoter> peerReviewVoterList = PeerReviewVoterBLL.GetModelList(" FPeerReviewID='" + PeerReviewModel[0].FPeerReviewID + "' and FStatus='Y' ");

                List<Model.XlsModel> xlsModels = new List<Model.XlsModel>();
                Model.XlsModel xm;

                xm = new Model.XlsModel();
                xm.Title = "序号";
                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                xm.RowIndex = 1;
                xm.RowSpan = 3;
                xlsModels.Add(xm);

                xm = new Model.XlsModel();
                xm.Title = "单位";
                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                xm.RowIndex = 1;
                xm.RowSpan = 3;
                xlsModels.Add(xm);

                xm = new Model.XlsModel();
                xm.Title = "姓名";
                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                xm.RowIndex = 1;
                xm.RowSpan = 3;

                xlsModels.Add(xm);

                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {
                    xm = new Model.XlsModel();
                    xm.Title = column.FRuleCls;
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.RowIndex = 1;
                    xm.ColSpan = PeerReviewRuleOptionList.Count + 1;
                    xlsModels.Add(xm);
                }

                xm = new Model.XlsModel();
                xm.Title = "合计";
                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                xm.RowIndex = 1;
                xm.RowSpan = 2;
                xm.ColSpan = PeerReviewRuleOptionList.Count;
                xlsModels.Add(xm);

                xm = new Model.XlsModel();
                xm.Title = "总分";
                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                xm.RowIndex = 1;
                xm.RowSpan = 3;
                xlsModels.Add(xm);

                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {

                    xm = new Model.XlsModel();
                    xm.Title = column.FRuleNote;
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.ColSpan = PeerReviewRuleOptionList.Count + 1;
                    xm.RowIndex = 2;
                    xlsModels.Add(xm);
                }

                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {
                    foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                    {
                        xm = new Model.XlsModel();
                        xm.Title = rowColum.FScoreName;
                        xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xm.RowIndex = 3;
                        xlsModels.Add(xm);
                    }
                    xm = new Model.XlsModel();
                    xm.Title = "得分";
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.RowIndex = 3;
                    xlsModels.Add(xm);
                }
                foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                {
                    xm = new Model.XlsModel();
                    xm.Title = rowColum.FScoreName + "合计";
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.RowIndex = 3;
                    xlsModels.Add(xm);
                }

                int i = 4;
                int count = 1;
                foreach (Model.PeerReviewBeenVoterCls cls in PeerReviewBeenVoterClsList)
                {
                    xm = new Model.XlsModel();
                    xm.Title = cls.FBeenVoterClsName;
                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                    xm.ColSpan = (PeerReviewRuleList.Count * PeerReviewRuleOptionList.Count + 4 + PeerReviewRuleList.Count + PeerReviewRuleOptionList.Count);
                    xm.RowIndex = i;
                    xlsModels.Add(xm);
                    i++;
                    List<Model.PeerReviewBeenVoter> selectPeerReviewBeenVoterList = (from nm in PeerReviewBeenVoterList where nm.FBeenVoterClsCd == cls.FBeenVoterClsCd select nm).ToList();

                    foreach (Model.PeerReviewBeenVoter row in selectPeerReviewBeenVoterList)
                    {
                        xm = new Model.XlsModel();
                        xm.Title = count.ToString();
                        xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xm.RowIndex = i;
                        xlsModels.Add(xm);
                        count++;
                        xm = new Model.XlsModel();
                        xm.Title = row.deptName;
                        xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xm.RowIndex = i;
                        xlsModels.Add(xm);

                        xm = new Model.XlsModel();
                        xm.Title = row.FEmpName;
                        xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                        xm.RowIndex = i;
                        xlsModels.Add(xm);
                        foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                        {
                            decimal sum = 0;
                            foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                            {
                                List<Models.PeerReviewQuery> select = (from nm in peerReviewResultList where nm.FBeenVoterEmplNo == row.FBeenVoterEmplNo && nm.FPeerReviewRuleCd == column.FPeerReviewRuleCd && rowColum.FPeerReviewRuleScoreCd == nm.FPeerReviewRuleScoreCd select nm).ToList();
                                if (select.Count > 0)
                                {
                                    xm = new Model.XlsModel();
                                    xm.Title = select[0].sumcount.ToString();
                                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                                    xm.RowIndex = i;
                                    xlsModels.Add(xm);
                                    sum += select[0].sumfen;
                                }
                                else
                                {
                                    xm = new Model.XlsModel();
                                    xm.Title = "0";
                                    xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                                    xm.RowIndex = i;
                                    xlsModels.Add(xm); 
                                }
                            }

                            xm = new Model.XlsModel();
                            if (peerReviewVoterList.Count > 0)
                            {
                                xm.Title = decimal.Round(sum / peerReviewVoterList.Count, 2).ToString() + "分";
                            }
                            else
                            {
                                xm.Title = "0分";
                            }
                            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                            xm.RowIndex = i;
                            xlsModels.Add(xm);
                        }

                        foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                        {
                            List<Models.PeerReviewQuery> select = (from nm in peerReviewResultList where nm.FBeenVoterEmplNo == row.FBeenVoterEmplNo && rowColum.FPeerReviewRuleScoreCd == nm.FPeerReviewRuleScoreCd select nm).ToList();
                            if (select.Count > 0)
                            {
                                xm = new Model.XlsModel();
                                xm.Title = select.Sum(t => t.sumcount).ToString();
                                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                                xm.RowIndex = i;
                                xlsModels.Add(xm);
                            }
                            else
                            {
                                xm = new Model.XlsModel();
                                xm.Title = "0";
                                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                                xm.RowIndex = i;
                                xlsModels.Add(xm); 
                            }
                        }

                        List<Models.PeerReviewQuery> selectSum = (from nm in peerReviewResultList where nm.FBeenVoterEmplNo == row.FBeenVoterEmplNo select nm).ToList();

                        if (selectSum.Count > 0)
                        {

                            xm = new Model.XlsModel();
                            if (peerReviewVoterList.Count > 0)
                            {
                                xm.Title = decimal.Round(selectSum.Sum(t => t.sumfen) / peerReviewVoterList.Count, 2).ToString() + "分";
                            }
                            else
                            {
                                xm.Title = "0分";
                            }
                            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                            xm.RowIndex = i;
                            xlsModels.Add(xm);
                        }
                        else
                        {
                            xm = new Model.XlsModel();
                            xm.Title = "0";
                            xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                            xm.RowIndex = i;
                            xlsModels.Add(xm); 
                        }
                        i++;
                    }
                }

                xm = new Model.XlsModel();
                xm.Title = "目前共 " + peerReviewVoterList.Count + " 人 参与投票";
                xm.HAlign = Model.XlsModel.HorizontalAlign.Center;
                xm.ColSpan = (PeerReviewRuleList.Count * PeerReviewRuleOptionList.Count + 4 + PeerReviewRuleList.Count + PeerReviewRuleOptionList.Count);
                xm.RowIndex = i;
                xlsModels.Add(xm);

                List<Model.XlsModel_Column> xlsModelColumns = new List<Model.XlsModel_Column>();
                Model.XlsModel_Column xmc = new Model.XlsModel_Column(); xmc.ColumnIndexEnd = 1; xmc.ColumnIndexStart = 0; xmc.Width = 20; xlsModelColumns.Add(xmc);
                arr = GenerateUtil.ExportToExcelReturnPathGetByte(".xls", PeerReviewModel[0].FTitle, xlsModels.Count + 1, xlsModels, xlsModelColumns, new List<Model.XlsModel_Row>());
            }


            return arr;
        }

        public string GetTable()
        {
            BLL.PeerReview PeerReviewBLL = new BLL.PeerReview();
            StringBuilder str = new StringBuilder();
            List<Model.PeerReview> PeerReviewModel = PeerReviewBLL.GetList();

            if (PeerReviewModel.Count > 0)
            {
                BLL.PeerReviewRule PeerReviewRuleBLL = new BLL.PeerReviewRule();
                List<Model.PeerReviewRule> PeerReviewRuleList = PeerReviewRuleBLL.GetModelList(" FPeerReviewID = '" + PeerReviewModel[0].FPeerReviewID + "'");

                BLL.PeerReviewRuleOption PeerReviewRuleOptionBLL = new BLL.PeerReviewRuleOption();
                List<Model.PeerReviewRuleOption> PeerReviewRuleOptionList = PeerReviewRuleOptionBLL.GetModelList("");

                BLL.PeerReviewBeenVoterCls PeerReviewBeenVoterClsBLL = new BLL.PeerReviewBeenVoterCls();
                List<Model.PeerReviewBeenVoterCls> PeerReviewBeenVoterClsList = PeerReviewBeenVoterClsBLL.GetAll();

                BLL.PeerReviewBeenVoter PeerReviewBeenVoterBLL = new BLL.PeerReviewBeenVoter();
                List<Model.PeerReviewBeenVoter> PeerReviewBeenVoterList = PeerReviewBeenVoterBLL.GetAllList("");

                BLL.PeerReviewResult PeerReviewResultBLL = new BLL.PeerReviewResult();
                DataSet dt = PeerReviewResultBLL.GetQueryList("");

                List<Models.PeerReviewQuery> peerReviewResultList = PeerReviewQueryGetList(dt.Tables[0]);

                BLL.PeerReviewVoter PeerReviewVoterBLL = new BLL.PeerReviewVoter();
                List<Model.PeerReviewVoter> peerReviewVoterList = PeerReviewVoterBLL.GetModelList(" FPeerReviewID='" + PeerReviewModel[0].FPeerReviewID + "' and FStatus='Y' ");

                str.Append("<table  border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"tableForm\">");

                str.Append("<tr>");
                str.Append("<td style='text-align:center'  style='width:60px'  class='tdClass' rowspan='3'>单位");
                str.Append("</td>");
                str.Append("<td style='text-align:center'  style='width:60px'  class='tdClass' rowspan='3'>姓名");
                str.Append("</td>");
                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {
                    str.Append("<td style='text-align:center'  class='tdClass' colspan='" + (PeerReviewRuleOptionList.Count + 1) + "'>");
                    str.Append(column.FRuleCls);
                    str.Append("</td>");
                }

                str.Append("<td style='text-align:center'  class='tdClass' colspan='" + PeerReviewRuleOptionList.Count + "'  rowspan='2''>");
                str.Append("合计");
                str.Append("</td>");


                str.Append("<td style='text-align:center'  class='tdClass' nowrap rowspan='3' >");
                str.Append("总分");
                str.Append("</td>");

                str.Append("</tr>");
                str.Append("<tr>");
                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {
                    str.Append("<td style='text-align:center'  class='tdClass' valign='top' colspan='" + (PeerReviewRuleOptionList.Count + 1) + "'>");
                    str.Append(column.FRuleNote);
                    str.Append("</td>");
                }

                str.Append("</tr>");

                str.Append("<tr>");
                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                {
                    foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                    {
                        str.Append("<td style='text-align:center'  class='tdClass' nowrap>");
                        str.Append(rowColum.FScoreName);
                        str.Append("</td>");
                    }
                    str.Append("<td style='text-align:center'  class='tdClass' class='tdClass' nowrap>");
                    str.Append("单项得分");
                    str.Append("</td>");
                }
                foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                {
                    str.Append("<td style='text-align:center' nowrap class='tdClass'>");
                    str.Append(rowColum.FScoreName + "合计");
                    str.Append("</td>");
                }
                str.Append("</tr>");

                foreach (Model.PeerReviewBeenVoterCls cls in PeerReviewBeenVoterClsList)
                {
                    str.Append("<tr>");

                    str.Append("<td style='text-align:center'  class='tdClass' colspan='" + (PeerReviewRuleList.Count * PeerReviewRuleOptionList.Count + 3 + PeerReviewRuleList.Count + PeerReviewRuleOptionList.Count) + "' >");
                    str.Append(cls.FBeenVoterClsName);
                    str.Append("</td>");
                    str.Append("</tr>");
                    List<Model.PeerReviewBeenVoter> selectPeerReviewBeenVoterList = (from nm in PeerReviewBeenVoterList where nm.FBeenVoterClsCd == cls.FBeenVoterClsCd select nm).ToList();

                    foreach (Model.PeerReviewBeenVoter row in selectPeerReviewBeenVoterList)
                    {
                        str.Append("<tr>");
                        str.Append("<td align='center' class='tdClass' nowrap>");
                        str.Append(row.deptName);
                        str.Append("</td>");
                        str.Append("<td align='center' class='tdClass' nowrap>");
                        str.Append(row.FEmpName);
                        str.Append("</td>");
                        foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                        {
                            decimal sum = 0;
                            foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                            {
                                List<Models.PeerReviewQuery> select = (from nm in peerReviewResultList where nm.FBeenVoterEmplNo == row.FBeenVoterEmplNo  && nm.FPeerReviewRuleCd == column.FPeerReviewRuleCd && rowColum.FPeerReviewRuleScoreCd == nm.FPeerReviewRuleScoreCd select nm).ToList();
                                if (select.Count > 0)
                                {
                                    str.Append("<td align='center' class='tdClass'>");
                                    str.Append(select[0].sumcount);
                                    str.Append("</td>");
                                    sum += select[0].sumfen;
                                }
                                else
                                {
                                    str.Append("<td align='center' class='tdClass'>");
                                    str.Append("0");
                                    str.Append("</td>");
                                }
                            }

                            str.Append("<td align='center' class='tdClass' nowrap>");
                            if (peerReviewVoterList.Count > 0)
                            {
                                str.Append(decimal.Round(sum / peerReviewVoterList.Count, 2));
                            }
                            else
                            {
                                str.Append("0");
                            }
                            str.Append("</td>");
                        }

                        foreach (Model.PeerReviewRuleOption rowColum in PeerReviewRuleOptionList)
                        {
                            List<Models.PeerReviewQuery> select = (from nm in peerReviewResultList where nm.FBeenVoterEmplNo == row.FBeenVoterEmplNo && rowColum.FPeerReviewRuleScoreCd == nm.FPeerReviewRuleScoreCd select nm).ToList();
                            if (select.Count > 0)
                            {
                                str.Append("<td align='center' class='tdClass' nowrap>");
                                str.Append(select.Sum(t => t.sumcount));
                                str.Append("</td>");
                            }
                            else
                            {
                                str.Append("<td align='center' class='tdClass' nowrap>");
                                str.Append("0");
                                str.Append("</td>");
                            }
                        }

                        List<Models.PeerReviewQuery> selectSum = (from nm in peerReviewResultList where nm.FBeenVoterEmplNo == row.FBeenVoterEmplNo select nm).ToList();

                        if (selectSum.Count > 0)
                        {
                            str.Append("<td align='center' class='tdClass' nowrap>");
                            if (peerReviewVoterList.Count > 0)
                            {
                                decimal sumall = 0;
                                foreach (Model.PeerReviewRule column in PeerReviewRuleList)
                                {
                                    List<Models.PeerReviewQuery> selectonly = (from nm in selectSum where column.FPeerReviewRuleCd == nm.FPeerReviewRuleCd select nm).ToList();
                                    sumall += (column.FScore * (selectonly.Sum(t => t.sumfen) / peerReviewVoterList.Count)) / 100;
                                }
                                str.Append(decimal.Round(sumall, 2));
                            }
                            else
                            {
                                str.Append("0");
                            }
                       
                            str.Append("</td>");
                        }
                        else
                        {
                            str.Append("<td align='center' class='tdClass' nowrap>");
                            str.Append("0");
                            str.Append("</td>");
                        }

                        str.Append("</tr>");
                    }
                }

                str.Append("<tr>");
                str.Append("<td align='center' class='tdClass'colspan='" + (PeerReviewRuleList.Count * PeerReviewRuleOptionList.Count + 3 + PeerReviewRuleList.Count + PeerReviewRuleOptionList.Count) + "'>");
                str.Append("目前共 " + peerReviewVoterList.Count + " 人 参与投票");
                str.Append("</td>");
                str.Append("</tr>");

                str.Append("</table>");
            }

            return str.ToString();
        }

    }
}
